package mya_dc.client.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;

import mya_dc.client.core.ClientSettings;
import mya_dc.client.gui.TextMessages.EMessageBoxType;

/**
 * authentication dialog
 * 
 * @author      Adam Levi
 * <br>			MYA
 */
public class AuthenticationDialog extends JDialog {
	private static final long serialVersionUID = -3911567405770980586L;
	private JPasswordField	passwordField;
	private JTextField 		projectNameTextField;
	private JTextField 		userNameTextField;
	private JCheckBox 		savePasswordCheckBox;
	private JLabel 			statusLabel;
	/**
	 * Create the dialog
	 */
	public AuthenticationDialog(
			String message,
			ClientSettings settings,
			Boolean result) {
		super();
		setResizable(false);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setModal(true);
		getContentPane().setLayout(null);
		setTitle("MYA DC Client Authentication");
		setBounds(100, 100, 332, 250);

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "Authentication Details", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		panel.setBounds(10, 34, 304, 142);
		getContentPane().add(panel);

		final JLabel userNameLabel = new JLabel();
		userNameLabel.setBounds(23, 56, 83, 21);
		userNameLabel.setText("User Name:");
		panel.add(userNameLabel);

		userNameTextField = new JTextField();
		userNameTextField.setBounds(112, 56, 157, 20);
		panel.add(userNameTextField);

		final JLabel projectNameLabel = new JLabel();
		projectNameLabel.setText("Project Name:");
		projectNameLabel.setBounds(23, 30, 83, 21);
		panel.add(projectNameLabel);

		projectNameTextField = new JTextField();
		projectNameTextField.setEnabled(false);
		projectNameTextField.setBounds(112, 30, 157, 20);
		panel.add(projectNameTextField);

		final JLabel userNameLabel_1 = new JLabel();
		userNameLabel_1.setText("Password:");
		userNameLabel_1.setBounds(23, 83, 83, 21);
		panel.add(userNameLabel_1);

		passwordField = new JPasswordField();
		passwordField.setBounds(112, 82, 157, 20);
		panel.add(passwordField);

		savePasswordCheckBox = new JCheckBox();
		savePasswordCheckBox.setText("Remember Password");
		savePasswordCheckBox.setBounds(112, 108, 157, 23);
		panel.add(savePasswordCheckBox);

		final JButton cancelButton = new JButton();
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				m_Canceled = true;
				dispose();
			}
		});
		cancelButton.setBounds(10, 182, 120, 30);
		getContentPane().add(cancelButton);
		cancelButton.setText("Cancel");

		final JButton okButton = new JButton();
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				doOK();

			}
		});
		okButton.setBounds(194, 182, 120, 30);
		getContentPane().add(okButton);
		okButton.setText("OK");

		statusLabel = new JLabel();
		statusLabel.setBounds(10, 10, 304, 18);
		getContentPane().add(statusLabel);
		//
		m_Settings = settings;
		initDialog(message);
	}
	
	
	private ClientSettings 	m_Settings;
	private boolean			m_Canceled;
	
	
	/**
	 * initialize the dialog
	 * 
	 * @param message - A message to show to the user.
	 */
	private void initDialog(String message)
	{
		statusLabel.setText(message);
		projectNameTextField.setText(m_Settings.projectName);
		userNameTextField.setText(m_Settings.username);
		passwordField.setText(m_Settings.password);
		savePasswordCheckBox.setSelected(m_Settings.savePassword);
	}
	
	/**
	 * tells if the dialog has been canceled
	 * 
	 * @return true is the user clicked on canceled
	 */
	public boolean isCanceled()
	{
		return m_Canceled;
	}
	
	/**
	 * updated the fields 
	 * 
	 * @return true is the user clicked on canceled
	 */
	private void doOK()
	{
		if (passwordField.getPassword().length==0)
		{
			TextMessages.showMessageBox(EMessageBoxType.ERROR_MISSING_PASSWORD, null);
			return;
		}
		m_Settings.savePassword = savePasswordCheckBox.isSelected();
		m_Settings.password = String.valueOf(passwordField.getPassword());
		m_Settings.username = userNameTextField.getText();
		m_Canceled = false;
		dispose();
	}
}
